package com.zues.haikangtempdemo.service;

import com.sun.jna.Pointer;
import com.zues.haikangtempdemo.config.HCNetSDK;
import lombok.extern.slf4j.Slf4j;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 报警信息回调
 */
@Slf4j
public class AlarmDataParse {

    public static void alarmDataHandle(int lCommand, HCNetSDK.NET_DVR_ALARMER pAlarmer, Pointer pAlarmInfo, int dwBufLen, Pointer pUser) {
        String sTime;
        String MonitoringSiteID;
        HCNetSDK.BYTE_ARRAY byte_array1 = new HCNetSDK.BYTE_ARRAY(25);
        byte_array1.write();
        Pointer pbyte_array1 = byte_array1.getPointer();
        pbyte_array1.write(0, pUser.getByteArray(0, byte_array1.size()), 0, byte_array1.size());
        byte_array1.read();
        //lCommand是传的报警类型
        switch (lCommand) {
            case HCNetSDK.COMM_THERMOMETRY_ALARM:  //温度报警信息
                HCNetSDK.NET_DVR_THERMOMETRY_ALARM struTemInfo = new HCNetSDK.NET_DVR_THERMOMETRY_ALARM();
                struTemInfo.write();
                Pointer pTemInfo = struTemInfo.getPointer();
                pTemInfo.write(0, pAlarmInfo.getByteArray(0, struTemInfo.size()), 0, struTemInfo.size());
                struTemInfo.read();
                log.info("温度报警！！ 规则ID:{}预置点号：{}报警等级：{}报警类型：{}当前温度：{}", struTemInfo.byRuleID,
                        struTemInfo.wPresetNo, struTemInfo.byAlarmLevel, struTemInfo.byAlarmType, struTemInfo.fCurrTemperature);

                //可见光图片保存
//                if ((struTemInfo.dwPicLen > 0) && (struTemInfo.byPicTransType == 0)) {
//                    SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");
//                    String newName = sf.format(new Date());
//                    FileOutputStream fout;
//              //温度图片保存
//                    try {
//                        String filename = "../pic/" + newName + "_" + struTemInfo.fCurrTemperature + ".jpg";
//                        fout = new FileOutputStream(filename);
//                        //将字节写入文件
//                        long offset = 0;
//                        ByteBuffer buffers = struTemInfo.pPicBuff.getByteBuffer(offset, struTemInfo.dwPicLen);
//                        byte[] bytes = new byte[struTemInfo.dwPicLen];
//                        buffers.rewind();
//                        buffers.get(bytes);
//                        fout.write(bytes);
//                        fout.close();
//                    } catch (FileNotFoundException e) {
//                        // TODO Auto-generated catch block
//                        e.printStackTrace();
//                    } catch (IOException e) {
//                        // TODO Auto-generated catch block
//                        e.printStackTrace();
//                    }
//                }

//                if ((struTemInfo.dwThermalPicLen > 0) && (struTemInfo.byPicTransType == 0)) {
//                    SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");
//                    String newName = sf.format(new Date());
//                    FileOutputStream fout;
//
//                    try {
//                        String filename = "../pic/" + newName + "_" + "_ThermalPiC" + ".jpg";
//                        fout = new FileOutputStream(filename);
//                        //将字节写入文件
//                        long offset = 0;
//                        ByteBuffer buffers = struTemInfo.pThermalPicBuff.getByteBuffer(offset, struTemInfo.dwThermalPicLen);
//                        byte[] bytes = new byte[struTemInfo.dwThermalPicLen];
//                        buffers.rewind();
//                        buffers.get(bytes);
//                        fout.write(bytes);
//                        fout.close();
//                    } catch (FileNotFoundException e) {
//                        // TODO Auto-generated catch block
//                        e.printStackTrace();
//                    } catch (IOException e) {
//                        // TODO Auto-generated catch block
//                        e.printStackTrace();
//                    }
//
//                }
                break;
            case HCNetSDK.COMM_THERMOMETRY_DIFF_ALARM: //温差检测报警
                HCNetSDK.NET_DVR_THERMOMETRY_DIFF_ALARM strThermDiffAlarm = new HCNetSDK.NET_DVR_THERMOMETRY_DIFF_ALARM();
                strThermDiffAlarm.write();
                Pointer pTemDiffInfo = strThermDiffAlarm.getPointer();
                pTemDiffInfo.write(0, pAlarmInfo.getByteArray(0, strThermDiffAlarm.size()), 0, strThermDiffAlarm.size());
                strThermDiffAlarm.read();
                String sThremDiffInfo = "通道号：" + strThermDiffAlarm.dwChannel + ",报警规则：" + strThermDiffAlarm.byAlarmRule + "，当前温差：" + strThermDiffAlarm.fCurTemperatureDiff;
                System.out.println(sThremDiffInfo);
                //可见光图片保存
                if ((strThermDiffAlarm.dwPicLen > 0) && (strThermDiffAlarm.byPicTransType == 0)) {
                    SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");
                    String newName = sf.format(new Date());
                    FileOutputStream fout;

                    try {
                        String filename = "../pic/" + newName + "_" + strThermDiffAlarm.fCurTemperatureDiff + ".jpg";
                        fout = new FileOutputStream(filename);
                        //将字节写入文件
                        long offset = 0;
                        ByteBuffer buffers = strThermDiffAlarm.pPicBuff.getByteBuffer(offset, strThermDiffAlarm.dwPicLen);
                        byte[] bytes = new byte[strThermDiffAlarm.dwPicLen];
                        buffers.rewind();
                        buffers.get(bytes);
                        fout.write(bytes);
                        fout.close();
                    } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                //热成像图片保存
                if ((strThermDiffAlarm.dwThermalPicLen > 0) && (strThermDiffAlarm.byPicTransType == 0)) {
                    SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");
                    String newName = sf.format(new Date());
                    FileOutputStream fout;

                    try {
                        String filename = "../pic/" + newName + "_" + "_ThermalDiffPiC" + ".jpg";
                        fout = new FileOutputStream(filename);
                        //将字节写入文件
                        long offset = 0;
                        ByteBuffer buffers = strThermDiffAlarm.pThermalPicBuff.getByteBuffer(offset, strThermDiffAlarm.dwThermalPicLen);
                        byte[] bytes = new byte[strThermDiffAlarm.dwThermalPicLen];
                        buffers.rewind();
                        buffers.get(bytes);
                        fout.write(bytes);
                        fout.close();
                    } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }

                }
                break;
            case HCNetSDK.COMM_FIREDETECTION_ALARM://烟火检测
                HCNetSDK.NET_DVR_FIREDETECTION_ALARM struFireDecAlarm = new HCNetSDK.NET_DVR_FIREDETECTION_ALARM();
                struFireDecAlarm.write();
                Pointer pFireDecAlarm = struFireDecAlarm.getPointer();
                pFireDecAlarm.write(0, pAlarmInfo.getByteArray(0, struFireDecAlarm.size()), 0, struFireDecAlarm.size());
                struFireDecAlarm.read();

                log.info("烟火检测报警！！绝对时间：{},报警子类型：{},火点最高温度 :{},火点目标距离：{}", struFireDecAlarm.dwAbsTime,
                        struFireDecAlarm.byAlarmSubType, struFireDecAlarm.wFireMaxTemperature, struFireDecAlarm.wTargetDistance);

                //可见光图片保存
//                if ((struFireDecAlarm.dwVisiblePicLen > 0) && (struFireDecAlarm.byPicTransType == 0)) {
//                    SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");
//                    String newName = sf.format(new Date());
//                    FileOutputStream fout;
//
//                    try {
//                        String filename = "../pic/" + newName + "_FireDecAlarm" + ".jpg";
//                        fout = new FileOutputStream(filename);
//                        //将字节写入文件
//                        long offset = 0;
//                        ByteBuffer buffers = struFireDecAlarm.pVisiblePicBuf.getByteBuffer(offset, struFireDecAlarm.dwVisiblePicLen);
//                        byte[] bytes = new byte[struFireDecAlarm.dwVisiblePicLen];
//                        buffers.rewind();
//                        buffers.get(bytes);
//                        fout.write(bytes);
//                        fout.close();
//                    } catch (FileNotFoundException e) {
//                        // TODO Auto-generated catch block
//                        e.printStackTrace();
//                    } catch (IOException e) {
//                        // TODO Auto-generated catch block
//                        e.printStackTrace();
//                    }
//                }
                //热成像图片保存
//                if ((struFireDecAlarm.dwPicDataLen > 0) && (struFireDecAlarm.byPicTransType == 0)) {
//                    SimpleDateFormat sf = new SimpleDateFormat("yyyyMMddHHmmss");
//                    String newName = sf.format(new Date());
//                    FileOutputStream fout;
//
//                    try {
//                        String filename = "../pic/" + newName + "_" + "_ThermalFireAlarm" + ".jpg";
//                        fout = new FileOutputStream(filename);
//                        //将字节写入文件
//                        long offset = 0;
//                        ByteBuffer buffers = struFireDecAlarm.pBuffer.getByteBuffer(offset, struFireDecAlarm.dwPicDataLen);
//                        byte[] bytes = new byte[struFireDecAlarm.dwPicDataLen];
//                        buffers.rewind();
//                        buffers.get(bytes);
//                        fout.write(bytes);
//                        fout.close();
//                    } catch (FileNotFoundException e) {
//                        // TODO Auto-generated catch block
//                        e.printStackTrace();
//                    } catch (IOException e) {
//                        // TODO Auto-generated catch block
//                        e.printStackTrace();
//                    }
//
//                }
                break;
            default:
                System.out.println("报警类型" + Integer.toHexString(lCommand));
                break;
        }
    }
}
